//
//  JSWebControllerManager.m
//  JSWebDemo
//
//  Created by ac-hu on 2018/7/6.
//  Copyright © 2018年 ac-hu. All rights reserved.
//

#import "JSWebControllerManager.h"


@implementation JSWebControllerManager

+(instancetype)share{
    static JSWebControllerManager *jsManager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (jsManager == nil) {
            jsManager = [[JSWebControllerManager alloc]init];
            [jsManager initProperty];
        }
    });
    return jsManager;
}

-(void)initProperty{
//    self.advancedControllers = @[].mutableCopy;
    self.controllers = @[].mutableCopy;
}

+(void)addContrllor:(UIViewController *)vc{
    [[JSWebControllerManager share].controllers addObject:vc];
}

+(void)remove:(UIViewController *)vc{
    [[JSWebControllerManager share].controllers removeObject:vc];
}

+(void)removeAllVC{
    [[JSWebControllerManager share].controllers removeAllObjects];
}

+(JSWebController *)currentVC{
    NSLog(@"%@",[JSWebControllerManager share].controllers);
   return [[JSWebControllerManager share].controllers lastObject];
}

//-(void)addController:(JSWebController *)controller WithIdentifier:(NSString *)identifier {
//    if ([self.controllers.allKeys containsObject:identifier]) {
//        [(NSMutableArray *)[self.controllers objectForKey:identifier] addObject:controller];
//    }else {
//        NSMutableArray * temp = [NSMutableArray array];
//        [temp addObject:controller];
//        [self.controllers setObject:temp forKey:identifier];
//    }
//}

+(void)nextPageWithUrl:(NSString *)url type:(NSInteger)type callback:(NSString *)callback{
    switch (type) {
        case 0:
            [JSWebControllerManager openSafariWithUrl:url];
            break;
        case 1:
            [JSWebControllerManager presentWebContrllorByUrl:url];
            break;
        case 2:
            [JSWebControllerManager pushWebContrllorByUrl:url];
            break;
        default:
            break;
    }
    
}

+(void)popViewController{
    
     [[JSWebControllerManager currentVC].navigationController popViewControllerAnimated:YES];
    if ([JSWebControllerManager share].controllers.count == 1) {
        [JSWebControllerManager removeAllVC];
    }
}

+(void)popToRootViewController{
    [[JSWebControllerManager currentVC].navigationController popToRootViewControllerAnimated:YES];
    [JSWebControllerManager removeAllVC];
}

+(void)popToViewController:(UIViewController *)viewController{
    [[JSWebControllerManager currentVC].navigationController popToViewController:viewController animated:YES];
}

+(void)pushWebContrllorByUrl:(NSString *)url{
    JSWebController * curVc = [JSWebControllerManager currentVC];
    if (curVc) {
        JSWebController *webCon = [[JSWebController alloc]initWithUrlStr:url];
        [curVc.navigationController pushViewController:webCon animated:YES];
    }
}

+(void)presentWebContrllorByUrl:(NSString *)url{
    JSWebController * curVc = [JSWebControllerManager currentVC];
    if (curVc) {
        JSWebController *webCon = [[JSWebController alloc]initWithUrlStr:url];
        [curVc presentViewController:webCon animated:YES completion:nil];
    }
}

+(void)openSafariWithUrl:(NSString *)url{
    NSURL *cleanURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@", url]];
    [[UIApplication sharedApplication] openURL:cleanURL];
}
@end
